package com.xj.admin.bussiness.registerLicense.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.Validator;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.bussiness.registerLicense.service.ITbRegisterLicenseService;
import com.xj.admin.util.JsonUtil;
import com.xj.admin.util.SessionUtil;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.registerLicense.entity.TbRegisterLicense;
import com.xj.common.bussiness.registerLicense.entity.registerLicenseMould;
import com.xj.common.bussiness.user.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-01-12
 */
@Controller
@RequestMapping("/registerLicense/")
public class TbRegisterLicenseController extends BaseController {

	@Autowired
	private ITbRegisterLicenseService registerLicenseService;
	
	@GetMapping("listUI")
    public String listUI() {
		return "registerLicense/list";
    }
	
	@GetMapping("form")
    public String form(Map<String,Object> map) {
		return "registerLicense/form";
    }
    
    @PostMapping("list")
	@ResponseBody
    public Object list(String gridPager) {
		Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
		Map<String, Object> parameters = null;
		if(Validator.isNullOrEmpty(pager.getParameters())){
			parameters = new HashMap<>();
		}else{
			parameters = pager.getParameters();
		}
		Page<TbRegisterLicense> list = registerLicenseService.selectPage(new Page<TbRegisterLicense>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(parameters));
		makeGridResult(parameters, pager, list);
		return parameters;
    }
	
	@PostMapping("save")
	@ResponseBody
	public AbstractBean add(TbRegisterLicense tbregisterlicense){
	    if (tbregisterlicense.getId() == null){
            tbregisterlicense.setCreateDate(new Date());
        }
		if(!registerLicenseService.insertOrUpdate(tbregisterlicense)){
			return error();
		}
		return success();
	}
	
	@DeleteMapping("{id}/delete")
	@ResponseBody
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!registerLicenseService.deleteById(id)){
			return error();
		}
		return success();
    }	
	
	@GetMapping("{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {	
		TbRegisterLicense tbregisterlicense = registerLicenseService.selectById(id);
		map.put("record", tbregisterlicense);
		return "registerLicense/edit";
    }

	@GetMapping("{factoryNum}/createTransferLicenseToForm")
	public String createTransferLicenseToForm(Map<String,Object> map,@PathVariable(required=true) String factoryNum){
		if(!factoryNum.equals("") && factoryNum !=null){
			TbRegisterLicense registerLicense = registerLicenseService.createTransferLicenseToForm(factoryNum);
			if(registerLicense != null){
				map.put("record", registerLicense);
				return "transferLicense/form";
			}else{
				return "registerLicense/list";
			}
		}else{
			return "registerLicense/list";
		}
	}

	@GetMapping("{id}/printPreview")
	public String printPreview(Map<String,Object> map,@PathVariable(required=true) Integer id){
		TbRegisterLicense tbregisterlicense = new TbRegisterLicense();
		tbregisterlicense.setId(id);
		System.out.println("id="+id);
//		TbUser user = SessionUtil.getloginUser();
//		System.out.println(user.toString());
		List<registerLicenseMould> registerLicenseList = registerLicenseService.searchRegisterLicenseList(tbregisterlicense);
		System.out.println("++++++++="+registerLicenseList);
		map.put("registerLicenseList", registerLicenseList);
		return "registerLicense/printPreview";
	}
}
